﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Report
{
    public class ScheduleModel
    {
        [DisplayName("Fecha deseada")]
        [Required(ErrorMessage = "Necesita poner una fecha")]
        public string TheDate { get; set; }

        public List<ScheduleInfo> Actions
        {
            get
            {
                return scheduleInfo;
            }
        }

        private List<ScheduleInfo> scheduleInfo;
        riskanalysisEntities entities = new riskanalysisEntities();

        public ScheduleModel()
        {
            scheduleInfo = new List<ScheduleInfo>();
        }

        public void Search()
        {
            DateTime theDate = Convert.ToDateTime(TheDate, Utils.culture);
            var query = from a in entities.actions
                        where a.programedDate == theDate
                        select new
                        {
                            Management = a.danger.activity.area.process.management.name,
                            Process = a.danger.activity.area.process.name,
                            Area = a.danger.activity.area.name,
                            Activity = a.danger.activity.name,
                            Danger = a.danger.description,
                            Action = a.description,
                            ProgrammedDate = a.programedDate,
                            ExecutionDate = a.executionDate,
                            Status = a.status
                        };

            scheduleInfo = new List<ScheduleInfo>();
            foreach (var item in query)
            {
                string executionDate = item.ExecutionDate.ToString("d", Utils.culture);

                executionDate = executionDate.Equals("01/01/2000") ? "Sin fecha" : executionDate;
                scheduleInfo.Add(
                    new ScheduleInfo
                    {
                        Action = item.Action,
                        Activity = item.Activity,
                        Area = item.Area,
                        Danger = item.Danger,
                        ProgrammedDate = item.ProgrammedDate.ToString("d", Utils.culture),
                        ExecutionDate = executionDate,
                        Management = item.Management,
                        Process = item.Process,
                        Status = item.Status.ToString()
                    });
            }
        }
    }
}